package edu.ccut.saturn.component.view;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.saturn.component.IView;

/**
 * 二进制流类型视图的实现类，保存了字节流类型的数据。
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.8.28
 * 
 * @see IView
 * @see HttpServletRequest
 * @see HttpServletResponse
 * 
 */
public class StreamView implements IView {

	private byte[] bytes;

	public StreamView(byte[] bytes) {
		this.bytes = bytes;
	}

	public void dispather(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			ServletOutputStream sout = response.getOutputStream();
			sout.write(this.bytes);
			sout.flush();
			
			if (sout != null) {
				sout.close();
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获得用于响应用户的字节流类型的数据
	 * @return
	 */
	public byte[] getBytes() {
		return bytes;
	}

	/**
	 * 设置用于响应用户的字节流类型的数据
	 * @param bytes
	 */
	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}
}